package com.sport.app.base;

import android.content.Context;
import android.os.Bundle;
import android.app.Activity;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.sport.app.R;

public class BaseActivity extends AppCompatActivity {

    private ProgressPopup mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
    }
    public void showProgress(@StringRes int strRes) {
        showProgress(getString(strRes));
    }

    public void showProgress() {
        showProgress(null);
    }

    public void showProgress(final String msg) {
        if (isFinishing()) {
            return;
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (null == mProgress) {
                    mProgress = ProgressPopup.show(BaseActivity.this, msg, true);
                }
                mProgress.setMessage(msg);
                mProgress.show();
            }
        });

    }

    public void hideProgress() {
        if (mProgress != null) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mProgress.dismiss();
                }
            });
        }
    }

    public void showToast(final String msg, final int duration) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), msg, duration).show();
            }
        });
    }

    public void showToast(final int msgRes, final int duration) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), msgRes, duration).show();
            }
        });
    }

    public Context getContext() {
        return this;
    }

    protected void hideKeyBoard() {
        if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
